package cn.lixin.hrm.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import java.lang.annotation.Inherited;

//资源服务配置
@Configuration
//开启资源服务配置
@EnableResourceServer
//开启方法授权
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class BaseResourceConfig extends ResourceServerConfigurerAdapter {
    @Value("${hrm.resourceId}") //从配置文件中获取值
    private String resourceId;
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        //配置resourceid
        resources.resourceId(resourceId);
        //配置校验token的方式
//        resources.tokenServices(tokenService());
        resources.tokenStore(tokenStore());
        // 开启无状态
        resources.stateless(true);
    }

    @Bean
    public TokenStore tokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter jwtAccessTokenConverter =
                new JwtAccessTokenConverter();
        // 类似 盐值
        jwtAccessTokenConverter.setSigningKey("111");
        return jwtAccessTokenConverter;
    }

    /* private ResourceServerTokenServices tokenService() {
         RemoteTokenServices tokenServices = new RemoteTokenServices();
         //要求请求中必须携带token
         tokenServices.setCheckTokenEndpointUrl("http://localhost:1070/oauth/check_token");
         //客户端id，对应认证服务的客户端详情配置的clientId
         tokenServices.setClientId("admin");
         //密钥，对应认证服务的客户端详情配置的clientId
         tokenServices.setClientSecret("1");
         return tokenServices;
     }
 */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/courseDoc/searchQuery").permitAll()
                .antMatchers("/employee/userinfo/**").permitAll()
                .antMatchers("/vipUser/register/**").permitAll()
                .antMatchers("/loginUser/register/**").permitAll()
                .antMatchers("/tenant/entering","/tenantType/list").permitAll()
                //校验scope必须为all ， 对应认证服务的客户端详情配置的clientId
                .antMatchers("/**").access("#oauth2.hasScope('hrm')")
                //关闭跨站伪造检查
                .and().csrf().disable()
                //把session设置为无状态，意思是使用了token，那么session不再做数据的记录
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

}